home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 9 / FM Towns Free Software Collection 9.iso / t_os / tool / tpsc256 / src / paltpack.c next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  4.2 KB  |  208 lines

  1. /*
  2.         TPSC256用 外部Shell
  3.         
  4.         パレットをソ-トする
  5.         使っている色を前に、使っていない色を後ろにソ-トする
  6.         
  7. */
  8.  
  9. #define EGBSIZE        1536    // EGB用ワ-クサイズ
  10. #define MOSSIZE        4096    // マウス用ワ-クサイズ
  11. #define BOOL        int        // 論理判断の型宣言
  12. #define TRUE        1        // 真
  13. #define FALSE        0        // 偽
  14. #define MENUPAGE    0        // メニュ-ペ-ジ番号
  15. #define DRAWPAGE    0        // 描画ペ-ジ番号
  16. #define SCREEN        12        // 画面モ-ド
  17. #define DRAWWIDE    640        // 処理画面横サイズ
  18. #define DRAWHIDE    480        // 処理画面縦サイズ
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <malloc.h>
  25. #include <math.h>
  26. #include <egb.h>
  27. #include <mos.h>
  28. #include <loader.h>            // コプロセス定義ファイル
  29.  
  30. char EGB_work[EGBSIZE];        // EGBワ-クエリア
  31. char MOS_work[MOSSIZE];        // マウスワ-クエリア
  32.  
  33. BOOL flg[256];
  34.  
  35. void GetPaltte( pal_all )
  36. char *pal_all;
  37. {
  38.     EGB_getPalette(DRAWPAGE,pal_all);
  39. }
  40.  
  41. void SetPaltte( pal_all )
  42. char *pal_all;
  43. {
  44.     EGB_palette(EGB_work,1,pal_all);
  45. }
  46.  
  47. void GetBlock(x1,y1,x2,y2,p)
  48. int x1,y1,x2,y2;
  49. unsigned int p;
  50. {
  51.     struct {
  52.         unsigned int    p ;
  53.         short    dseg, x1, y1, x2, y2 ;
  54.     } pgetput ;
  55.     pgetput.p    = p ;
  56.     pgetput.dseg = getds();
  57.     pgetput.x1   = x1 ;        pgetput.y1   = y1 ;
  58.     pgetput.x2   = x2 ;        pgetput.y2   = y2 ;
  59.     EGB_getBlock(EGB_work,(char *)&pgetput);
  60. }
  61.  
  62. void PutBlock( x1,y1,x2,y2,p )
  63. int x1,y1,x2,y2;
  64. unsigned int p;
  65. {
  66.     struct {
  67.         unsigned int    p ;
  68.         short    dseg, x1, y1, x2, y2 ;
  69.     } pgetput ;
  70.     pgetput.p    = p ;
  71.     pgetput.dseg = getds();
  72.     pgetput.x1   = x1 ;        pgetput.y1   = y1 ;
  73.     pgetput.x2   = x2 ;        pgetput.y2   = y2 ;
  74.     EGB_putBlock(EGB_work,0,(char *)&pgetput);
  75. }
  76.  
  77. void Line(x1,y1,x2,y2)
  78. int x1,y1,x2,y2;
  79. {
  80. struct {     unsigned short int n;
  81.             short int x1,y1,x2,y2; } par;
  82.     par.n = 2;
  83.     par.x1 = x1; par.y1 = y1;
  84.     par.x2 = x2; par.y2 = y2;
  85.     EGB_connect(EGB_work,(char *)&par);
  86. }
  87.  
  88. void ColorFind()
  89. {
  90.     int x,y;
  91.     int col;
  92.     char *buf;
  93.     
  94.     buf = malloc( DRAWWIDE );
  95.     for(y=0;y<DRAWHIDE;y++) {
  96.         GetBlock(0,y,DRAWWIDE-1,y,buf);
  97.         EGB_writeMode(EGB_work,4);
  98.         Line(0,y,DRAWWIDE-1,y);
  99.         for(x=0;x<DRAWWIDE;x++) {
  100.             col = buf[x];
  101.             flg[col] = TRUE;
  102.         }
  103.         Line(0,y,DRAWWIDE-1,y);
  104.         EGB_writeMode(EGB_work,0);
  105.     }
  106.     free( buf );
  107. }
  108.  
  109. void PaltteSort()
  110. {
  111.     int ch,mx,my;
  112.     int x,y;
  113.     int i,j;
  114.     int ary[256];
  115.     int col;
  116.     char *oldpalt,*newpalt;
  117.     char *buf;
  118.     
  119.     ary[0] = 0;
  120.     ary[255] = 255;
  121.     j = 1;
  122.     for(i=1;i<255;i++) {
  123.         if( flg[i] == TRUE ) {
  124.             ary[i] = j;
  125.             j++;
  126.         }
  127.     }
  128.     for(i=1;i<255;i++) {
  129.         if( flg[i] != TRUE ) {
  130.             ary[i] = j;
  131.             j++;
  132.         }
  133.     }
  134.     oldpalt= malloc( 256*8+4 );
  135.     newpalt= malloc( 256*8+4 );
  136.     GetPaltte(oldpalt);
  137.     memcpy(newpalt,oldpalt,256*8+4);
  138.     for(i=0;i<256;i++) {
  139.         j = ary[i];
  140.         newpalt[j*8  +4+4] = oldpalt[i*8  +4+4];
  141.         newpalt[j*8+1+4+4] = oldpalt[i*8+1+4+4];
  142.         newpalt[j*8+2+4+4] = oldpalt[i*8+2+4+4];
  143.     }
  144.     buf = (char *)malloc( DRAWWIDE*DRAWHIDE );
  145.     if( buf == NULL ) {
  146.         SetPaltte(newpalt);
  147.         buf = (char *)malloc( DRAWWIDE );
  148.         for(y=0;y<DRAWHIDE;y++) {
  149.             MOS_rdpos(&ch,&mx,&my);
  150.             if( ch != 0 ) break;
  151.             GetBlock(0,y,DRAWWIDE-1,y,buf);
  152.             for(x=0;x<DRAWWIDE;x++) {
  153.                 col = buf[x];
  154.                 buf[x] = ary[col];
  155.             }
  156.             PutBlock(0,y,DRAWWIDE-1,y,buf);
  157.         }
  158.     } else {
  159.         GetBlock(0,0,DRAWWIDE-1,DRAWHIDE-1,buf);
  160.         for(x=0;x<DRAWWIDE*DRAWHIDE;x++) {
  161.             col = buf[x];
  162.             buf[x] = ary[col];
  163.         }
  164.         SetPaltte(newpalt);
  165.         PutBlock(0,0,DRAWWIDE-1,DRAWHIDE-1,buf);
  166.     }
  167.     free( oldpalt );
  168.     free( newpalt );
  169.     free( buf );
  170. }
  171.  
  172. main()
  173. {
  174.     ADDRESS temp;
  175.     int i,x,y;
  176.     int page[2];
  177.     
  178.     pcl_get_dta(&temp);
  179.     
  180.     /* 画面・マウスの初期化 */
  181.     EGB_getResolution(&page[0], &page[1]);
  182.     if( page[0] != SCREEN ) goto quit;
  183.     
  184.     EGB_resolution(EGB_work,DRAWPAGE,page[0]|0x40);        // Page0 Init
  185.     EGB_displayPage(EGB_work,0,1);                        // 1page view
  186.     MOS_start(MOS_work,MOSSIZE);                        // マウス初期化
  187.     MOS_resolution(DRAWPAGE,page[0]);
  188.     
  189.     EGB_writePage(EGB_work,DRAWPAGE);    // 描画ペ-ジを指定
  190.     EGB_paintMode(EGB_work,0x22);        // ペイントモ-ドを指定
  191.     EGB_color(EGB_work,0,255);            // ドットの色を指定
  192.     EGB_writeMode(EGB_work,0);
  193.     
  194.     for(i=0;i<256;i++)
  195.         flg[i] = FALSE;                // フラグの初期化
  196.     
  197.     /* 処理開始 */
  198.     ColorFind();
  199.     PaltteSort();
  200.     
  201.     /* 子プロセスの終了処理 */
  202.     MOS_end();
  203.  
  204. quit:
  205.     pcl_exit(0);
  206.     return (0);
  207. }
  208.